<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use app\admin\model\Auths;
use app\admin\model\Danb;
use think\Db;
use think\Request;

class Danbiao
{
    /**加载列表
     * @param Request $request
     * @return \think\response\View
     */
    public function show_list(Request $request){
        $modelid = $request->param('modelid',null);
        $ziduan = Danb::show($modelid);
        $model =Db::name('model')->where('id',$modelid)->find();
        $ziduan['model']=$model;
        return view('list',$ziduan);
    }

    /**加载添加页面
     * @param Request $request
     * @return string|\think\response\View
     */
    public function show_add(Request $request){
        $modelid = $request->param('modelid',null);
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        if(!$modelid){
            return '请输入添加数据的表id';
        }
        $str = Danb::show_add($modelid);
        return view('add',['str'=>$str,'modelid'=>$modelid]);
    }

    /**单表添加
     * @param Request $request
     * @return \think\response\View
     */
    public function add(Request $request){
        $data = $request->post();
        $bool = Danb::add_d($data,$request->file());
//        echo '<pre>';
//        print_r($bool);exit;
        if($bool){
           return view('document/err',['str'=>'添加成功','code'=>200]);
        }else{
            return view('document/err',['str'=>'添加失败','code'=>201]);
        }
    }

    /**单表编辑
     * @param Request $request
     * @return string|\think\response\View
     */
    public function edit(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入要修改的id';
        }
        $modelid = $request->param('modelid',null);
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        if(!$modelid){
            return '请输入要修改的id';
        }
        $str = Danb::edit_d($id,$modelid);
        return view('edit',['str'=>$str]);
    }

    /**更新单表数据
     * @param Request $request
     * @return \think\response\View
     */
    public function update(Request $request){
        $data = $request->post();
        $file = $request->file();
        $bool = Danb::update_d($data,$file);
        if($bool){
            return view('document/err',['str'=>'更新成功','code'=>200]);
        }else{
            return view('document/err',['str'=>'更新失败','code'=>201]);
        }
    }

    /**删除单表数据
     * @param Request $request
     * @return string|\think\response\View
     */
    public function del(Request $request){
        $modelid = $request->param('modelid',null);
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        if(!$modelid){
            return '请输入删除的数据的id';
        }
        $id = $_POST['id'];
        if(is_array($id)){
         foreach ($id as $value){
             $bool = Danb::delete($value,$modelid);
             if(!$bool){
                 return '删除失败';
             }
         }
        }else{
            $bool = Danb::delete($id,$modelid);
            if(!$bool){
                return '删除失败';
            }
        }
        return 1;
    }
}